var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import { $wuxPrompt } from '../../components/wux';

var app = getApp();
var that;


Page({
  data: { 
    user:null,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false,
    type:1,
    code:"",
    isall:0
    ,str:"我的竞赛"
  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onPullDownRefresh:function(){
    util.showLoading();
    that.loadData();
  },
  onLoad: function (options) {
    that = this;
    
    if( !util.judge.isNull( options.isall) ){
        that.data.isall = parseInt(options.isall);
    }

    that.data._sdesc="我的竞赛";
    if(that.data.isall ==1){
        that.data._sdesc="所有竞赛";
        that.setData({
            isall :1
        });
        
    }
    wx.setNavigationBarTitle( { title: that.data._sdesc });
    
    $wuxPrompt.init('msg1', {
      title: '暂时没有相关数据', 
    }).show()
	

	   util.showLoading();
    app.getUserInfo()
    .then(res => {
      that.setData({
        user:res
      })
      //util.alert(that.data.user.uid);
      that.loadData(); 
    });
	
  },
 
  onReady : function(){
   
  },
  onReachBottom : function(e,a){
      
  },
  
  loadData: function(){

      var url = config.url_user_team;
      if(that.data.isall ==1){
          url = config.url_team_list_all;
          
      }

    var params = {
      code:""
    }

    wxRequest.postRequest(url, params)
    .then(res=>{

      for(var i in res){
        var now = new Date().getTime();
        var bd = util.str2date(res[i].bdate).getTime();
        var ed = util.str2date(res[i].edate).getTime();
        var ld = util.str2date(res[i].ldate).getTime();
        
        //判断
        var status = 0;
        var str="未开始";
        
        if(now  <  bd){
          status = 0;
           str="未开始";
        }else if( now >= bd && now <= ed )
        {
          status = 1;
          str="进行中";
        }
        else if( now > ed && now <= ld )
        {
          status = 2;
          str="评奖中";
        }
        else{
          status = 3;
          str="已结束";
        }

        var isclose = res[i].isclose;
        if(isclose ==1){
            status = 3;
            str="已关闭";
        }

        res[i].str = str;
        res[i].status = status;
        res[i].bdate = util.date2str(res[i].bdate,"MM.dd");
        res[i].edate = util.date2str(res[i].edate,"MM.dd");
      }

		  that.setData({
        list : res
      });

      if(res.length ==0){
        that.setData({
          isEmpty :true
        });
      }

      
    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
      wx.stopPullDownRefresh();
    });
  },
  onedit:function(e){
    var data = e.currentTarget.dataset;
    var  tid = data.id;

    wx.navigateTo({
		url: '../team/edit?tid='+tid
    })
    
  }
    ,onrow:function(e){
        var data = e.currentTarget.dataset;
        util.showLoading();

        var url = config.url_team_user_is;
        var params = {
            id:data.id
        }
        wxRequest.postRequest(url, params)
        .then(res => {
            if(util.judge.isNull(res)){
                wx.navigateTo({
                    url: '../team/view?tid='+data.id
                })
      
            }else{
                wx.navigateTo({
                    url: '../team/main?tid='+ data.id
                });
            }
        })
        .catch(res => {
            util.alert(res.message);
        })
        .finally(function (res) {
            util.hideLoading();
        });
    }
    ,onty:function(e){
        var data = e.currentTarget.dataset;
        wx.navigateTo({
            url: '../team/glty?tid='+ data.id
        });
    }
    ,ongb:function(e){
        var data = e.currentTarget.dataset;
        util.confirm("确定要关闭竞赛吗?关闭后将不能再次开启!",function(res){
            if (res.confirm) {
                util.showLoading();
                var url = config.url_team_close;
                var params = {
                    tid: data.id
                }
                wxRequest.postRequest(url, params)
                .then(res => {
                    if(res==1){
                        that.onPullDownRefresh();
                        throw util.stop();
                    }else{
                        util.alert("操作失败");
                    }
                
                })
                .catch(res => {
                    util.alert_catch(res.message);
                })
                .finally(function (res) {
                    util.hideLoading();
                });
            }
        });
    }

})


